//获取省市区ID对应名称缓存文件
var locationKeyValue = require('../../../staticData/locationKeyValue.json')

exports.list = (req,res) => {
    
    //获取参数信息
    var bodyParams    = req.routeInfo.bodyParams;
    var restfulParams = req.routeInfo.restfulParams;

    var resultData = [];

    //获取合作单位信息
    function searchCooperation(cb){

        //搜索参数拼接
        var searchStr = "";

        if(JSON.stringify(bodyParams) != '{}'){

            //根据省份搜索
            if(bodyParams.pid != 0 && bodyParams.pid != undefined){
                searchStr += "/osei_provinces/" + bodyParams.pid;
            }

            //根据城市搜索
            if(bodyParams.cid != 0 && bodyParams.cid != undefined){
                searchStr += "/osei_city/" + bodyParams.cid;
            }

            //根据区域搜索
            if(bodyParams.aid != 0 && bodyParams.aid != undefined){
                searchStr += "/osei_area/" + bodyParams.aid;
            }

            //根据名称
            if(bodyParams.cooperation != "" && bodyParams.cooperation != undefined){
                searchStr += "/os_name/" + encodeURI(bodyParams.cooperation);
            }

            //根据主节点ID
            if(bodyParams.osid != ""){
                searchStr += "/os_id/" + bodyParams.osid;
            }

        }

        R.SEND_HTTP(
            req,
            res,
            {
                url: CORE_APIConfig.coreAPI.organizationStructureExtensionInfoSearch.Request_Api_Url + "/no/os_master_type-0/os_fid/"+restfulParams.master_id+"/os_is_delete/0" + searchStr,
                method : 'POST'
            },
            function (data) {

                resultData = data.result;

                cb();
                
            }
        )

    }

    //转化省市区ID为名称
    function conversionLocationName(cb){

  
        for(var i=0;i<resultData.data.length;i++){

            resultData.data[i].pName = resultData.data[i].osei_provinces == "0" ? "" : locationKeyValue[resultData.data[i].osei_provinces];
            resultData.data[i].cName = resultData.data[i].osei_city      == "0" ? "" : locationKeyValue[resultData.data[i].osei_city];
            resultData.data[i].aName = resultData.data[i].osei_area      == "0" ? "" : locationKeyValue[resultData.data[i].osei_area];

        }

        cb();

    }

    //计算下级节点数
    function calculateChildNodeCount(cb){

        for(var i=0;i<resultData.data.length;i++){

            resultData.data[i].childCount = 0;

        }

        cb();

    }

    //计算成交客户数
    function calculateCustomerCount(cb){

        for(var i=0;i<resultData.data.length;i++){

            resultData.data[i].customerCount = 0;

        }

        cb();

    }


    F.Serial([searchCooperation,conversionLocationName,calculateChildNodeCount,calculateCustomerCount],() => {
        RES.successResponse(res,resultData);
    })
}